<template>
  <div class="clothing-transfer-page">
    <div class="page-header">
      <h1 class="page-title">服装迁移</h1>
      <p class="page-description">AI智能将一张图片的服装样式迁移到另一张图片上，保持人物特征完整</p>
    </div>

    <div class="main-content">
      <!-- 设置面板 -->
      <div class="settings-panel">
        <div class="setting-group">
          <h3 class="group-title">上传图片</h3>
          
          <!-- 目标人物图片 -->
          <div class="upload-section">
            <label class="upload-label">目标人物</label>
            <div class="upload-area" @click="triggerUpload('person')">
              <input 
                ref="personInput" 
                type="file" 
                accept="image/*" 
                @change="handleImageUpload($event, 'person')"
                style="display: none"
              />
              <div class="upload-content">
                <div class="upload-icon">👤</div>
                <p>上传人物图片</p>
                <span class="upload-hint">需要换装的人物</span>
              </div>
            </div>
          </div>

          <!-- 服装样式图片 -->
          <div class="upload-section">
            <label class="upload-label">服装样式</label>
            <div class="upload-area" @click="triggerUpload('clothing')">
              <input 
                ref="clothingInput" 
                type="file" 
                accept="image/*" 
                @change="handleImageUpload($event, 'clothing')"
                style="display: none"
              />
              <div class="upload-content">
                <div class="upload-icon">👗</div>
                <p>上传服装图片</p>
                <span class="upload-hint">参考的服装样式</span>
              </div>
            </div>
          </div>
        </div>

        <div class="setting-group">
          <h3 class="group-title">迁移设置</h3>
          
          <div class="transfer-options">
            <div class="option-item">
              <label class="option-label">迁移类型</label>
              <div class="option-buttons">
                <button 
                  v-for="type in transferTypes" 
                  :key="type.id"
                  :class="['option-btn', { active: selectedType === type.id }]"
                  @click="selectType(type.id)"
                >
                  <span class="option-icon">{{ type.icon }}</span>
                  <span class="option-name">{{ type.name }}</span>
                </button>
              </div>
            </div>

            <div class="option-item">
              <label class="option-label">保持度</label>
              <div class="setting-slider-group">
                <input 
                  v-model="fidelity" 
                  type="range" 
                  min="0" 
                  max="100" 
                  class="setting-slider"
                />
                <span class="slider-value">{{ fidelity }}%</span>
              </div>
            </div>

            <div class="option-item">
              <label class="option-label">融合强度</label>
              <div class="setting-slider-group">
                <input 
                  v-model="blendStrength" 
                  type="range" 
                  min="0" 
                  max="100" 
                  class="setting-slider"
                />
                <span class="slider-value">{{ blendStrength }}%</span>
              </div>
            </div>
          </div>
        </div>

        <div class="setting-group">
          <div class="action-controls">
            <button 
              class="action-btn primary large" 
              @click="startTransfer" 
              :disabled="!canTransfer || isProcessing"
            >
              {{ isProcessing ? '生成中...' : '开始迁移' }}
            </button>
            <button class="action-btn" @click="clearAll" :disabled="isProcessing">
              清空重置
            </button>
            <button class="action-btn" @click="downloadResult" :disabled="!resultImage">
              下载结果
            </button>
          </div>
        </div>
      </div>

      <!-- 预览区域 -->
      <div class="preview-section">
        <!-- 输入图片预览 -->
        <div class="input-previews">
          <div class="preview-card">
            <h4 class="preview-title">目标人物</h4>
            <div class="image-container">
              <div v-if="!personImage" class="empty-placeholder">
                <div class="placeholder-icon">👤</div>
                <p>请上传人物图片</p>
              </div>
              <img v-else :src="personImage" alt="目标人物" class="preview-img" />
            </div>
          </div>

          <div class="transfer-indicator">
            <div class="transfer-arrow">
              <div class="arrow-line"></div>
              <div class="arrow-head">→</div>
            </div>
            <div class="transfer-text">AI迁移</div>
          </div>

          <div class="preview-card">
            <h4 class="preview-title">服装样式</h4>
            <div class="image-container">
              <div v-if="!clothingImage" class="empty-placeholder">
                <div class="placeholder-icon">👗</div>
                <p>请上传服装图片</p>
              </div>
              <img v-else :src="clothingImage" alt="服装样式" class="preview-img" />
            </div>
          </div>
        </div>

        <!-- 结果预览 -->
        <div class="result-preview">
          <h4 class="preview-title">迁移结果</h4>
          <div class="result-container">
            <div v-if="!resultImage && !isProcessing" class="empty-result">
              <div class="empty-icon">✨</div>
              <p>点击"开始迁移"生成结果</p>
            </div>
            
            <div v-if="isProcessing" class="processing-state">
              <div class="processing-animation">
                <div class="processing-circle"></div>
                <div class="processing-circle"></div>
                <div class="processing-circle"></div>
              </div>
              <p class="processing-text">AI正在进行服装迁移...</p>
              <div class="progress-bar">
                <div class="progress-fill" :style="{ width: progress + '%' }"></div>
              </div>
              <span class="progress-text">{{ progress }}%</span>
            </div>

            <div v-if="resultImage" class="result-display">
              <img :src="resultImage" alt="迁移结果" class="result-img" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ClothingTransfer',
  data() {
    return {
      personImage: null,
      clothingImage: null,
      resultImage: null,
      isProcessing: false,
      progress: 0,
      selectedType: 'full',
      fidelity: 80,
      blendStrength: 70,
      
      transferTypes: [
        { id: 'full', name: '全身服装', icon: '👔' },
        { id: 'upper', name: '上身服装', icon: '👕' },
        { id: 'lower', name: '下身服装', icon: '👖' },
        { id: 'accessories', name: '配饰', icon: '👜' }
      ]
    }
  },
  computed: {
    canTransfer() {
      return this.personImage && this.clothingImage;
    }
  },
  methods: {
    triggerUpload(type) {
      if (type === 'person') {
        this.$refs.personInput.click();
      } else if (type === 'clothing') {
        this.$refs.clothingInput.click();
      }
    },
    
    handleImageUpload(event, type) {
      const file = event.target.files[0];
      if (file) {
        const reader = new FileReader();
        reader.onload = (e) => {
          if (type === 'person') {
            this.personImage = e.target.result;
          } else if (type === 'clothing') {
            this.clothingImage = e.target.result;
          }
          this.resultImage = null; // 清除之前的结果
        };
        reader.readAsDataURL(file);
      }
    },
    
    selectType(typeId) {
      this.selectedType = typeId;
    },
    
    async startTransfer() {
      if (!this.canTransfer) return;
      
      this.isProcessing = true;
      this.progress = 0;
      this.resultImage = null;
      
      // 模拟AI处理进度
      const progressInterval = setInterval(() => {
        this.progress += Math.random() * 10;
        if (this.progress >= 90) {
          this.progress = 90;
          clearInterval(progressInterval);
        }
      }, 400);
      
      // 模拟AI处理过程
      setTimeout(() => {
        clearInterval(progressInterval);
        this.progress = 100;
        this.resultImage = this.generateMockResult();
        this.isProcessing = false;
      }, 4000);
    },
    
    generateMockResult() {
      // 创建模拟的服装迁移结果
      const canvas = document.createElement('canvas');
      const ctx = canvas.getContext('2d');
      
      // 基于人物图片创建结果
      const personImg = new Image();
      personImg.onload = () => {
        canvas.width = personImg.width;
        canvas.height = personImg.height;
        
        // 绘制原始人物图片
        ctx.drawImage(personImg, 0, 0);
        
        // 添加一些视觉效果模拟服装迁移
        ctx.fillStyle = 'rgba(255, 192, 203, 0.2)'; // 粉色透明层
        ctx.fillRect(0, personImg.height * 0.3, personImg.width, personImg.height * 0.4);
        
        // 添加边框效果
        ctx.strokeStyle = 'rgba(255, 0, 0, 0.3)';
        ctx.lineWidth = 3;
        ctx.strokeRect(0, personImg.height * 0.3, personImg.width, personImg.height * 0.4);
      };
      personImg.src = this.personImage;
      
      return canvas.toDataURL();
    },
    
    clearAll() {
      this.personImage = null;
      this.clothingImage = null;
      this.resultImage = null;
      this.isProcessing = false;
      this.progress = 0;
      this.$refs.personInput.value = '';
      this.$refs.clothingInput.value = '';
    },
    
    downloadResult() {
      if (this.resultImage) {
        const link = document.createElement('a');
        link.download = 'clothing_transfer_result.png';
        link.href = this.resultImage;
        link.click();
      }
    }
  }
}
</script>

<style scoped>
.clothing-transfer-page {
  padding: 1.5rem;
  min-height: 100vh;
  background: linear-gradient(135deg, #e8f5e8 0%, #f0f8ff 100%);
}

.page-header {
  text-align: center;
  margin-bottom: 2rem;
}

.page-title {
  font-size: 2.5rem;
  font-weight: bold;
  color: #2d3748;
  margin-bottom: 1rem;
}

.page-description {
  font-size: 1.1rem;
  color: #718096;
  max-width: 600px;
  margin: 0 auto;
}

.main-content {
  display: flex;
  gap: 2rem;
  max-width: 1400px;
  margin: 0 auto;
}

.settings-panel {
  width: 350px;
  background: white;
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  height: fit-content;
}

.setting-group {
  margin-bottom: 2rem;
}

.group-title {
  font-size: 1.2rem;
  font-weight: 600;
  color: #2d3748;
  margin-bottom: 1rem;
}

.upload-section {
  margin-bottom: 1.5rem;
}

.upload-label {
  display: block;
  font-size: 0.9rem;
  font-weight: 500;
  color: #4a5568;
  margin-bottom: 0.5rem;
}

.upload-area {
  border: 2px dashed #68d391;
  border-radius: 12px;
  padding: 1.5rem;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  background: #f0fff4;
}

.upload-area:hover {
  border-color: #48bb78;
  background-color: #edf7ed;
}

.upload-icon {
  font-size: 2rem;
  margin-bottom: 0.5rem;
}

.upload-hint {
  font-size: 0.8rem;
  color: #a0aec0;
}

.transfer-options {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.option-item {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.option-label {
  font-size: 0.9rem;
  font-weight: 500;
  color: #4a5568;
}

.option-buttons {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
}

.option-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.75rem;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;
  gap: 0.25rem;
}

.option-btn:hover {
  border-color: #68d391;
  background-color: #f0fff4;
}

.option-btn.active {
  border-color: #68d391;
  background-color: #edf7ed;
  color: #276749;
}

.option-icon {
  font-size: 1.25rem;
}

.option-name {
  font-size: 0.8rem;
  font-weight: 500;
}

.setting-slider-group {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.setting-slider {
  flex: 1;
  height: 6px;
  border-radius: 3px;
  background: #e2e8f0;
  outline: none;
}

.slider-value {
  font-size: 0.875rem;
  font-weight: 600;
  color: #68d391;
  min-width: 45px;
}

.action-controls {
  display: grid;
  gap: 0.75rem;
}

.action-btn {
  padding: 0.875rem 1rem;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.action-btn.primary {
  background: linear-gradient(135deg, #68d391 0%, #48bb78 100%);
  color: white;
}

.action-btn.primary.large {
  padding: 1rem 1.5rem;
  font-size: 1.1rem;
}

.action-btn.primary:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(104, 211, 145, 0.4);
}

.action-btn:not(.primary) {
  background: #f0fff4;
  color: #276749;
  border: 1px solid #68d391;
}

.action-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.preview-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

.input-previews {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 1.5rem;
  align-items: center;
}

.preview-card {
  background: white;
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.preview-title {
  font-size: 1.1rem;
  font-weight: 600;
  color: #2d3748;
  margin-bottom: 1rem;
  text-align: center;
}

.image-container {
  width: 250px;
  height: 300px;
  border-radius: 12px;
  overflow: hidden;
  position: relative;
  background: #f7fafc;
  border: 2px dashed #e2e8f0;
}

.empty-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #a0aec0;
}

.placeholder-icon {
  font-size: 3rem;
  margin-bottom: 1rem;
}

.preview-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.transfer-indicator {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

.transfer-arrow {
  display: flex;
  align-items: center;
  color: #68d391;
}

.arrow-line {
  width: 40px;
  height: 2px;
  background: #68d391;
}

.arrow-head {
  font-size: 1.5rem;
  font-weight: bold;
}

.transfer-text {
  font-size: 0.9rem;
  font-weight: 600;
  color: #48bb78;
  background: #f0fff4;
  padding: 0.5rem 1rem;
  border-radius: 20px;
  border: 1px solid #68d391;
}

.result-preview {
  background: white;
  border-radius: 16px;
  padding: 2rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.result-container {
  width: 100%;
  height: 400px;
  border-radius: 12px;
  overflow: hidden;
  position: relative;
  background: #f7fafc;
  border: 2px dashed #e2e8f0;
}

.empty-result {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #a0aec0;
}

.empty-icon {
  font-size: 4rem;
  margin-bottom: 1rem;
}

.processing-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 1.5rem;
}

.processing-animation {
  display: flex;
  gap: 0.5rem;
}

.processing-circle {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #68d391;
  animation: bounce 1.4s ease-in-out infinite both;
}

.processing-circle:nth-child(1) { animation-delay: -0.32s; }
.processing-circle:nth-child(2) { animation-delay: -0.16s; }

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1); }
}

.processing-text {
  font-weight: 600;
  color: #2d3748;
}

.progress-bar {
  width: 200px;
  height: 8px;
  background: #e2e8f0;
  border-radius: 4px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #68d391, #48bb78);
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 0.9rem;
  font-weight: 600;
  color: #68d391;
}

.result-display {
  width: 100%;
  height: 100%;
}

.result-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .main-content {
    flex-direction: column;
  }
  
  .settings-panel {
    width: 100%;
  }
  
  .input-previews {
    grid-template-columns: 1fr;
    gap: 1rem;
  }
  
  .transfer-indicator {
    transform: rotate(90deg);
  }
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }
  
  .option-buttons {
    grid-template-columns: 1fr;
  }
  
  .image-container {
    width: 100%;
    height: 250px;
  }
}
</style> 
